﻿Imports System.Web
Imports System.Web.SessionState
Imports System.Data.OleDb

Public Class Global_asax
    Inherits System.Web.HttpApplication

    Private Shared totalNumberOfUsers As Integer = 0
    Private Shared currentNumberOfUsers As Integer = 0

    Dim conn = New OleDbConnection(ConfigurationManager.ConnectionStrings("PlacesConnectionString").ToString())
    

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application is started

        Dim sql As String = "SELECT counterValue from counterTable"

        Dim dbcmd As OleDbCommand = New OleDbCommand(sql, conn)

        conn.open()

        Dim reader As OleDbDataReader = dbcmd.ExecuteReader()

        'Dim newCount As Integer

        If (reader.Read()) Then
            totalNumberOfUsers = (reader("counterValue"))
        Else
            totalNumberOfUsers = 0

        End If
        reader.Close()
        conn.Close()




    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session is started

        Dim sql As String = "UPDATE counterTable SET counterValue = " & totalNumberOfUsers

        Dim dbcmd = New OleDbCommand(sql, conn)

        totalNumberOfUsers += 1
        currentNumberOfUsers += 1

        conn.Open()
        dbcmd.ExecuteNonQuery()
        conn.Close()
    End Sub

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires at the beginning of each request
    End Sub

    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires upon attempting to authenticate the use
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when an error occurs
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session ends
        currentNumberOfUsers -= 1
    End Sub

    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application ends
    End Sub

    Public Shared ReadOnly Property TotalUsers() As Integer
        Get
            Return TotalNumberOfUsers
        End Get
    End Property
    Public Shared ReadOnly Property CurrentUsers() As Integer
        Get
            Return CurrentNumberOfUsers
        End Get
    End Property
End Class